package cn.wolfcode.p2p.business.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.business.domain.UserBankinfo;
import cn.wolfcode.p2p.business.mapper.UserBankinfoMapper;
import cn.wolfcode.p2p.business.service.IUserBankinfoService;

@Service
@Transactional
public class UserBankinfoServiceImpl implements IUserBankinfoService {

	@Autowired
	private UserBankinfoMapper mapper;

	@Autowired
	private IUserInfoService userinfoService;

	@Override
	public UserBankinfo getByUserId(Long id) {
		return this.mapper.selectByUserId(id);
	}

	@Override
	public void bind(UserBankinfo bi) {
		// 判断没有绑定银行卡
		UserInfo current = this.userinfoService.get(UserContext.getCurrent().getId());
		if (!current.getHasBindBankInfo()) {
			// 设置相关信息并保存;
			bi.setAccountName(current.getRealName());
			bi.setUserInfoId(current.getId());
			this.mapper.insert(bi);
			// 添加状态吗
			current.addState(BitStateUtil.OP_BIND_BANKINFO);
			this.userinfoService.update(current);
		}
	}

}
